<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="问卷名称" prop="title">
        <el-input
          v-model="queryParams.title"
          placeholder="请输入问卷名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="问卷类型" prop="formType">
        <el-select v-model="queryParams.formType" placeholder="请选择问卷类型" clearable>
          <el-option
            v-for="dict in dict.type.que_form_type"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
          <el-option
            v-for="dict in dict.type.sys_yes_no"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['secondVitim:queForm:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['secondVitim:queForm:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['secondVitim:queForm:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['secondVitim:queForm:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="queFormList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="id" align="center" prop="id" />
      <el-table-column label="问卷名称" align="center" prop="title" />
      <el-table-column label="问卷类型" align="center" prop="formType">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.que_form_type" :value="scope.row.formType"/>
        </template>
      </el-table-column>
      <el-table-column label="问卷描述" align="center" prop="content" >
        <template slot-scope="scope">
          <div v-html="scope.row.content" class="content-preview"></div>
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.status" active-value="0" inactive-value="1" @change="handleStatusChange(scope.row)"></el-switch>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['secondVitim:queForm:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['secondVitim:queForm:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改调查问卷名称对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="问卷名称" prop="title">
          <el-input v-model="form.title" placeholder="请输入问卷名称" />
        </el-form-item>
        <el-form-item label="问卷类型" prop="formType">
          <el-select v-model="form.formType" placeholder="请选择问卷类型">
            <el-option
              v-for="dict in dict.type.que_form_type"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="问卷描述">
          <editor v-model="form.content" :min-height="192"/>
        </el-form-item>
        <el-form-item label="停用" prop="status">
          <el-radio-group v-model="form.status">
            <el-radio
              v-for="dict in dict.type.sys_yes_no"
              :key="dict.value"
              :label="dict.value"
            >{{dict.label}}</el-radio>
          </el-radio-group>
        </el-form-item>
        <el-divider content-position="center">问卷细项信息</el-divider>
        <el-row :gutter="10" class="mb8">
          <el-col :span="1.5">
            <el-button type="primary" icon="el-icon-plus" size="mini" @click="handleAddQueFormDetail">添加</el-button>
          </el-col>
          <el-col :span="1.5">
            <el-button type="danger" icon="el-icon-delete" size="mini" @click="handleDeleteQueFormDetail">删除</el-button>
          </el-col>
        </el-row>
        <el-table :data="queFormDetailList" :row-class-name="rowQueFormDetailIndex" @selection-change="handleQueFormDetailSelectionChange" ref="queFormDetail">
          <el-table-column type="selection" width="50" align="center" />
          <el-table-column label="序号" align="center" prop="index" width="50"/>
          <el-table-column label="题目类型" prop="type" width="150">
            <template slot-scope="scope">
              <el-select v-model="scope.row.type" placeholder="请选择">
                <el-option
                  v-for="dict in dict.type.que_form_detail_type"
                  :key="dict.value"
                  :label="dict.label"
                  :value="dict.value"
                ></el-option>
              </el-select>
            </template>
          </el-table-column>
          <el-table-column label="问题" prop="title" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.title" placeholder="请输入问题" />
            </template>
          </el-table-column>
          <el-table-column label="排序" prop="orderNum" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.orderNum" placeholder="请输入排序" />
            </template>
          </el-table-column>
          <!-- <el-table-column label="选项，多选项使用$分隔" prop="options" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.options" placeholder="请输入选项，多选项使用$分隔" />
            </template>
          </el-table-column> -->
         <!-- <el-table-column label="$comment" prop="status" width="150">
            <template slot-scope="scope">
              <el-select v-model="scope.row.status" placeholder="请选择$comment">
                <el-option label="请选择字典生成" value="" />
              </el-select>
            </template>
          </el-table-column> -->
          <el-table-column label="是否必填" prop="required" width="150">
            <template slot-scope="scope">
               <el-select v-model="scope.row.required" placeholder="请选择">
                 <el-option
                   v-for="dict in dict.type.question_is_required"
                   :key="dict.value"
                   :label="dict.label"
                   :value="dict.value"
                 ></el-option>
               </el-select>
            </template>
          </el-table-column>
         <!-- <el-table-column label="$comment" prop="remark" width="150">
            <template slot-scope="scope">
              <el-input v-model="scope.row.remark" placeholder="请输入$comment" />
            </template>
          </el-table-column> -->
        </el-table>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listQueForm, getQueForm, delQueForm, addQueForm, updateQueForm, changeStatus} from "@/api/secondVitim/queForm"

export default {
  name: "QueForm",
  dicts: ['sys_yes_no', 'que_form_type', 'que_form_detail_type', 'question_is_required'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 子表选中数据
      checkedQueFormDetail: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 调查问卷名称表格数据
      queFormList: [],
      // 问卷细项表格数据
      queFormDetailList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        title: null,
        formType: null,
        content: null,
        status: null
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    }
  },
  created() {
    this.getList()
  },
  methods: {
    /** 查询调查问卷名称列表 */
    getList() {
      this.loading = true
      listQueForm(this.queryParams).then(response => {
        this.queFormList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    // 用户状态修改
    handleStatusChange(row) {
      debugger
      let text = row.status === "0" ? "启用" : "停用"
      this.$modal.confirm('确认要"' + text + '""' + row.title + '"问卷吗？').then(function() {
        return changeStatus(row.id, row.status)
      }).then(() => {
        this.$modal.msgSuccess(text + "成功")
      }).catch(function() {
        row.status = row.status === "0" ? "1" : "0"
      })
    },
    // 取消按钮
    cancel() {
      this.open = false
      this.reset()
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        title: null,
        formType: null,
        content: null,
        createTime: null,
        createBy: null,
        updateTime: null,
        updateBy: null,
        status: null
      }
      this.queFormDetailList = []
      this.resetForm("form")
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm")
      this.handleQuery()
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset()
      this.open = true
      this.title = "添加调查问卷名称"
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset()
      const id = row.id || this.ids
      getQueForm(id).then(response => {
        this.form = response.data
        this.queFormDetailList = response.data.queFormDetailList
        this.open = true
        this.title = "修改调查问卷名称"
      })
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          this.form.queFormDetailList = this.queFormDetailList
          if (this.form.id != null) {
            updateQueForm(this.form).then(response => {
              this.$modal.msgSuccess("修改成功")
              this.open = false
              this.getList()
            })
          } else {
            addQueForm(this.form).then(response => {
              this.$modal.msgSuccess("新增成功")
              this.open = false
              this.getList()
            })
          }
        }
      })
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids
      this.$modal.confirm('是否确认删除调查问卷名称编号为"' + ids + '"的数据项？').then(function() {
        return delQueForm(ids)
      }).then(() => {
        this.getList()
        this.$modal.msgSuccess("删除成功")
      }).catch(() => {})
    },
	/** 问卷细项序号 */
    rowQueFormDetailIndex({ row, rowIndex }) {
      row.index = rowIndex + 1
    },
    /** 问卷细项添加按钮操作 */
    handleAddQueFormDetail() {
      let obj = {}
      obj.type = ""
      obj.title = ""
      obj.orderNum = ""
      obj.options = ""
      obj.status = ""
      obj.required = ""
      obj.remark = ""
      this.queFormDetailList.push(obj)
    },
    /** 问卷细项删除按钮操作 */
    handleDeleteQueFormDetail() {
      if (this.checkedQueFormDetail.length == 0) {
        this.$modal.msgError("请先选择要删除的问卷细项数据")
      } else {
        const queFormDetailList = this.queFormDetailList
        const checkedQueFormDetail = this.checkedQueFormDetail
        this.queFormDetailList = queFormDetailList.filter(function(item) {
          return checkedQueFormDetail.indexOf(item.index) == -1
        })
      }
    },
    /** 复选框选中数据 */
    handleQueFormDetailSelectionChange(selection) {
      this.checkedQueFormDetail = selection.map(item => item.index)
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('secondVitim/queForm/export', {
        ...this.queryParams
      }, `queForm_${new Date().getTime()}.xlsx`)
    }
  }
}
</script>
